/*
 * 请求相关,异步api同步封装
 */
 

import {HTTP_REQUEST_URL} from '../config.js' 

const request = {} 

/*
 * opts:请求参数 url,data,datatype,method, 
 * opts:{
	methods:// 请求方式
	url:    // 请求地址
	data:   // 请求参数 
 }
 * 
 * 
 * 
 */

request.globalRequest = (opts) => {   
	let contentType = "application/x-www-form-urlencoded"; 
	if (opts.type && opts.type == 2) {
	    contentType = "application/json";
	} 
	let data={
		// appid: '100024',
		// token: uni.getStorageSync('token')
	}
	if(opts.data){
		data=Object.assign(opts.data, data)
	} 
	return new Promise((resolve, reject) => {
		uni.request({
			url:opts.url,
			method:opts.methods,
			data:data,
			dataType:'json',
			timeout: 30000,
			header: {
			  "Content-Type": contentType,
			  'token': uni.getStorageSync('token') 
			}, 
			success: (res) => {	
				if(res.data.code==1002){
					uni.showToast({
						icon:'error',
						mask:true,
						title:'登录已失效',
						duration:2000,
						success() {
							setTimeout(()=>{
								uni.reLaunch({
									url:'/pages/login/login'
								})
							},500)							
						}
					})	
				}else if(res.data.code==1009){
					uni.showToast({
						icon:'none',
						mask:true,
						title:res.data.msg,
						duration:2000,						 
					})	
				}else{
					resolve(res);
				}				
				
			},
			fail: (err) => {	 
				uni.showModal({
					content:opts.url,
					cancelText:'取消',
					confirmText:'刷新重试',
					success(res) {
						if(res.confirm){
							
						}
					}
				})
				 
				reject(err)
			}
		})
	})
}

request.vPost = (opts) => {  
	let contentType = "application/x-www-form-urlencoded";
	if (opts.type && opts.type == 2) {
	    contentType = "application/json";
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url:opts.url,
			method:'post',
			data:opts.data,
			dataType:'json',
			timeout: 30000,
			header: {
			  "Content-Type": contentType,
			  'Cookie': uni.getStorageSync('cookiekey') 
			}, 
			success: (res) => {				
				resolve(res);
			},
			fail: (err) => {	 
				uni.showModal({
					content:'网络请求失败，请稍后在试369?',
					cancelText:'取消',
					confirmText:'刷新重试',
					success(res) {
						if(res.confirm){
							
						}
					}
				})
				 
				reject(err)
			}
		})
	})
}

request.vGet = (opts,type) => {  
	let contentType = "application/x-www-form-urlencoded";
	if (opts.type&&opts.type == 2) {
	    contentType = "application/json";
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url:opts.url,
			method:'get',
			data:opts.data,
			dataType:'json',
			timeout: 30000,
			header: {
			  "Content-Type": contentType,
			  'Cookie': uni.getStorageSync('cookiekey') 
			}, 
			success: (res) => {				
				resolve(res);
			},
			fail: (err) => {	 
				uni.showModal({
					content:'网络请求失败，请稍后在试159',
					cancelText:'取消',
					confirmText:'刷新重试',
					success(res) {
						if(res.confirm){
							
						}
					}
				})
				 
				reject(err)
			}
		})
	})
}

/*
 * opts:请求参数 url,filePath,name,formData, 
 * opts:{
	filePath(string):// 本地资源路径
	name(string):    // 上传文件键名
	formData(object):   // 其他参数  
 }
 * 
 * 
 * 
 */
request.uploadFile = (opts) => {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url:opts.url,
			filePath:opts.filePath,
			name:opts.name,
			header: {
			  "Content-Type": "multipart/form-data",
			  'Cookie': uni.getStorageSync('cookiekey') 
			},
			formData:opts.data,
			success: (res) => {
				resolve(res);
			},
			fail: (err) => {				 
				uni.showModal({
					content:'网络请求失败，请稍后在试789',
					cancelText:'取消',
					confirmText:'刷新重试',
					success(res) {
						if(res.confirm){
							
						}
					}
				})
				 
				reject(err)
			}
		})
	})
}


export default request

 













